package org.blindmandesign;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

/**
 *
 * @author Nathan Bubna
 * @since javaQuery 1.0
 * @version $Id$
 */
public class Text extends Node<Text> {

    private Object content;

    public NodeType getType() {
        return NodeType.TEXT;
    }

    public Text(Object content) {
        this.content = content;
    }

    public Text text(Object content) {
        this.content = content;
        return this;
    }

    public Object text() {
        return content;
    }

    public boolean isEmpty() {
        return content == null || content.toString().length() == 0;
    }

    public boolean matches(Node node) {
        if (node == null) {
            return true;
        }
        if (content == null) {
            return false;
        }
        return node.toString().equals(content.toString());
    }

    public void render(StringBuilder s) {
        if (!isEmpty()) {
            s.append(content);
        }
    }

    public void render(StringBuilder s, Config config) {
        // don't actually need a Config...
        render(s);
    }

    public void render(String indent, StringBuilder s, Config config) {
        if (!isEmpty()) {
            if (indent != null && s.length() > 0) {
                s.append(indent);
            }
            s.append(content);
        }
    }

    public String toString() {
        StringBuilder s = new StringBuilder();
        render(s);
        return s.toString();
    }

}
